// @good
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');
const Good = require('../../models/Goods/Good');
const Goods_type = require('../../models/Goods/Goods_type');
const User_Good = require('../../models/User/User_good');
const Equipment = require('../../models/Equipments/Equipment');
const Equipment_type = require('../../models/Equipments/Equipment_type');
const User_equipment = require('../../models/User/User_equipment');
const open_api = require('../../controller/open_api');
const good_api = require('../../controller/good_api');
const user_api = require('../../controller/user_api');



// @route  GET api/goods/getallgoods
// @desc   返回的请求的json数据
// @access public
router.get('/getallgoods', good_api.getallGoodInfo);


// @route  POST api/goods/addgood
// @desc   新增道具
// @access public
router.post('/add', (req, res) => {
  Good.findOne({ name: req.body.name }).then(name => {
    if (name) {
      return res.status(400).json('已存在该物品');
    } else {
      console.log('addreq', req.body);
      const newGood = {};
      if (req.body.name) { newGood.name = req.body.name };
      if (req.body.info) { newGood.info = req.body.info };
      if (req.body.img) { newGood.icon = req.body.img };
      if (req.body.type) { newGood.type = req.body.type };
      if (req.body.rare_level) { newGood.rare_level = req.body.rare_level };
      if (req.body.price) { newGood.price = req.body.price };
      if (req.body.is_sell) { newGood.is_sell = req.body.is_sell };
      if (req.body.is_system_sell) { newGood.is_system_sell = req.body.is_system_sell };
      if (req.body.use_type) { newGood.use_type = req.body.use_type };
      if (req.body.skill) { newGood.skill = req.body.skill };
      if (req.body.goods_type) { newGood.goods_type = req.body.goods_type };
      if (req.body.monster) { newGood.monster = req.body.monster };
      if (req.body.task) { newGood.task = req.body.task };
      if (req.body.status) { newGood.goods_type = req.body.status };
      new Good(newGood).save()
        .then(good => res.json(good))
        .catch(err => console.log(err));
    }
  });
});

// @route  POST api/goods/get_goods_type/edit
// @desc   编辑物品类型信息接口
// @access Private
router.post(
  '/edit/:id',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    console.log("edit", req.body);
    const newGood = {};
    if (req.body.name) { newGood.name = req.body.name };
    if (req.body.info) { newGood.info = req.body.info };
    if (req.body.img) { newGood.icon = req.body.img };
    if (req.body.type) { newGood.type = req.body.type };
    if (req.body.rare_level) { newGood.rare_level = req.body.rare_level };
    if (req.body.price) { newGood.price = req.body.price };
    if (req.body.is_sell) { newGood.is_sell = req.body.is_sell };
    if (req.body.is_system_sell) { newGood.is_system_sell = req.body.is_system_sell };
    if (req.body.use_type) { newGood.use_type = req.body.use_type };
    if (req.body.skill) { newGood.skill = req.body.skill };
    if (req.body.goods_type) { newGood.goods_type = req.body.goods_type };
    if (req.body.monster) { newGood.monster = req.body.monster };
    if (req.body.task) { newGood.task = req.body.task };
    if (req.body.status) { newGood.goods_type = req.body.status };
    Good.findOneAndUpdate(
      { _id: req.params.id },
      { $set: newGood },
      { new: true }
    ).then(goods => res.json(goods));
  }
);

// @route  POST api/goods/delete/:id
// @desc   删除物品信息接口
// @access Private
router.delete(
  '/delete/:id',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    Good.findOneAndRemove({ _id: req.params.id })
      .then(goods => res.json(goods))
      .catch(err => res.status(404).json('删除失败!'));
  }
);


// @route  GET api/goods/types/get
// @desc   获取物品类型信息
// @access Private

router.get(
  '/types/get',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    Goods_type.find()
      .then(goods_type => {
        if (!goods_type) {
          return res.status(404).json('没有任何内容');
        }
        res.json(goods_type);
      })
      .catch(err => res.status(404).json(err));
  }
);

// @route  Post api/goods/types/add
// @desc   新增物品类型接口
// @access Private

router.post(
  '/types/add',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    const Goods_type_Fields = {};
    if (req.body.name) Goods_type_Fields.name = req.body.name;
    if (req.body.info) Goods_type_Fields.info = req.body.info;
    new Goods_type(Goods_type_Fields).save().then(goods_type => {
      res.json(goods_type);
    });
  }
);

// @route  POST api/goods/types/edit/:id
// @desc   编辑物品类型信息接口
// @access Private
router.post(
  '/types/edit/:id',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    const Goods_type_Fields = {};

    if (req.body.name) Goods_type_Fields.name = req.body.name;
    if (req.body.info) Goods_type_Fields.info = req.body.info;

    Goods_type.findOneAndUpdate(
      { _id: req.params.id },
      { $set: Goods_type_Fields },
      { new: true }
    ).then(goods_type => res.json(goods_type));
  }
);

// @route  POST api/goods/types/delete/:id
// @desc   删除物品类型信息接口
// @access Private
router.delete(
  '/types/delete/:id',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    Goods_type.findOneAndRemove({ _id: req.params.id })
      .then(goods_type => {
        res.json(goods_type);
      })
      .catch(err => res.status(404).json('删除失败!'));
  }
);

// @route  POST api/goods/usergood/add/
// @desc   增加玩家背包物品接口
// @access Private
router.post(
  '/usergood/add/:id',
  passport.authenticate('jwt', { session: false }),
  user_api.addusergood
);

// @route  get api/goods/usergood/get/
// @desc   获取玩家背包物品接口
// @access Private
router.get(
  '/usergood/get',
  passport.authenticate('jwt', { session: false }),
  user_api.getusergood
);



module.exports = router;
